package com.bawei.newsapplication.view.adapter

import com.bawei.newsapplication.R

/**
 * @author : 谢庆平
 * @date   : 2024/10/16 17:18
 * @desc   :
 */
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.coom.model.bean.fomm.NewsListBean
import com.bumptech.glide.Glide

class MyAdapter(private val context: Context,
                private val list: List<NewsListBean.Result>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View?
        return when (viewType) {
            1 -> {
                view = LayoutInflater.from(context).inflate(R.layout.fronpagelist_item, parent, false)
                ViewHolder1(view)
            }
            2 -> {
                view = LayoutInflater.from(context).inflate(R.layout.frontitle_item, parent, false)
                ViewHolder2(view)
            }
            3 -> {
                view = LayoutInflater.from(context).inflate(R.layout.pagelistttitle_item, parent, false)
                ViewHolder3(view)
            }
            else -> {
                view = LayoutInflater.from(context).inflate(R.layout.fronpagelist_item, parent, false)
                ViewHolder1(view)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val split = list[position].thumbnailUrl.split(";")
        when (holder) {
            is ViewHolder1 -> {
                // 设置 ViewHolder1 的内容
            }
            is ViewHolder2 -> {
                (holder as ViewHolder2).textView.text = list[position].title
                setImg(split[0], holder.imageView)
            }
            is ViewHolder3 -> {
                (holder as ViewHolder3).textView.text = list[position].title
                setImg(split[0], holder.img1)
                setImg(split[1], holder.img2)
                setImg(split[2], holder.img3)
            }
        }
    }

    override fun getItemCount(): Int = list.size

    override fun getItemViewType(position: Int): Int {
        val split = list[position].thumbnailUrl.split(";")
        return when {
            split.isEmpty() -> 1
            split.size == 1 -> 2
            split.size > 1 -> 3
            else -> 2
        }
    }

    private fun setImg(imgUrl: String, imageView: ImageView) {
        Glide.with(context)
            .load(imgUrl)
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .into(imageView)
    }

    inner class ViewHolder1(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.front_name)
    }

    inner class ViewHolder2(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.frontlist_name)
        val imageView: ImageView = itemView.findViewById(R.id.fronlist_img1)
    }

    inner class ViewHolder3(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.fronpage_name3)
        val img1: ImageView = itemView.findViewById(R.id.fronpage_img)
        val img2: ImageView = itemView.findViewById(R.id.fronpage_img2)
        val img3: ImageView = itemView.findViewById(R.id.fronpage_img3)
    }
}